package com.hyp.pay.UI.Home.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizeBag;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.hyp.pay.R;
import com.hyp.pay.UI.Home.presenter.Edit2Presenter;
import com.hyp.pay.UI.Home.presenter.EditPresenter;
import com.hyp.pay.UI.Home.presenter.contract.Edit2Contract;
import com.hyp.pay.UI.Home.presenter.contract.EditContract;
import com.hyp.pay.base.BaseActivity;
import com.hyp.pay.device.N900Device;
import com.hyp.pay.http.Constants;
import com.hyp.pay.http.HyqApis;
import com.hyp.pay.model.FindRecharge;
import com.hyp.pay.model.UserJson;
import com.hyp.pay.utils.LinePathView;
import com.hyp.pay.utils.SPUtil;
import com.hyp.pay.utils.SystemUtil;
import com.hyp.pay.weight.ProgressDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class EditPaymentActivity extends BaseActivity<Edit2Presenter> implements View.OnClickListener,Edit2Contract.View {

    SharedPreferences sp;
    @BindView(R.id.clean)
    TextView mClean;
    @BindView(R.id.view)
    LinePathView mView;
    @BindView(R.id.time)
    TextView mTime;
    @BindView(R.id.sure_qian)
    Button mSureQian;
    private ProgressDialog mDialog;
    private Handler mHandler;
    private static final int PHOTOS_SUCCESS =  200;
    private static final int PHOTOS_FAILURE =  500;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_edit_payment;
    }

    @Override
    protected void initView() {
        mClean.setOnClickListener(this);
        mSureQian.setOnClickListener(this);
        mDialog = new ProgressDialog(this);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        mTime.setText(str);

        mPresenter.getFindRecharge(SPUtil.getOrder());
        mView.setBackColor(getResources().getColor(R.color.c_f1f1f1));

    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.clean:
                mView.setPenColor(Color.BLACK);
                mView.setBackColor(getResources().getColor(R.color.c_f1f1f1));
                mView.clear();
                break;
            case R.id.sure_qian:

                if (mView.getTouched()) {
                    // 获取内置SD卡路径
                    String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                    // 图片文件路径
                    String filePath = sdCardPath + File.separator + "screenshot.png";
                    File file = new File(filePath);
                    Log.d("olj", file.length() + "");
                    try {
                        mView.save(filePath,false,0);
                        SPUtil.setSignPayervName(filePath);
                        Log.i("olj", "付款人签名" + filePath);
                        Toast.makeText(this, "签名保存成功", Toast.LENGTH_SHORT).show();
                        mSureQian.setEnabled(false);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    screenshot();
                } else {
                    Toast.makeText(this, "请签名", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void screenshot() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                // 获取屏幕
                final View dView = getWindow().getDecorView();
                dView.setDrawingCacheEnabled(true);
                dView.buildDrawingCache();
                Bitmap bmp = dView.getDrawingCache();
                if (bmp != null) {
                    try {
                        // 获取内置SD卡路径
                        String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                        // 图片文件路径
                        String filePath = sdCardPath + File.separator + "short.png";

                        File file = new File(filePath);
                        FileOutputStream fos = new FileOutputStream(file);
                        if (null != fos) {
                            boolean success = bmp.compress(Bitmap.CompressFormat.PNG, 30, fos);
                            fos.flush();
                            fos.close();
                            if (success)
                                SPUtil.setSignPayerDiv(filePath);
                            Log.i("olj", "付款人截屏" + filePath);
                            Toast.makeText(EditPaymentActivity.this, "截屏成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(EditPaymentActivity.this, EditActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        Looper.loop();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }




    @Override
    public void showError(String msg) {

    }

    @Override
    public void showFindRecharge(FindRecharge data) {
        if (data.getCode().equals("200")){
            String id=data.getHyqAccountRecharge().getId();
            SPUtil.seId(id);
        }

    }
}

